package com.hellowx.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * Created by IntelliJ IDEA.
 *  自定义全局filter
 * @author MagicMushroom
 * @date 2021/7/14
 */
@Configuration
public class CustomerGlobalFilter implements GlobalFilter, Ordered {

    /**
     * 类似于javaWeb doFilter
     * @param exchange 交换，封装了 request，response
     * @param chain 放行
     * @return filter
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        System.out.println("经过全局Filter处理");
        Mono<Void> filter = chain.filter(exchange);
        System.out.println("响应回来，filter处理");
        return filter;
    }

    /**
     *  用来指定自定义filter执行顺序
     * @return 数字 -1 最先执行
     */
    @Override
    public int getOrder() {
        return 0;
    }
}
